--[[

shared_functions.lua

These functions are shared by the various other lua files.

]]

-- Function: Find out the real player name, that is player+realm
function LPS_SF_GetPlayerRealmName()
	local realmName = GetCVar("realmName");

	local playerName = UnitName("player");
	if ((playerName ~= nil) and (playerName ~= UNKNOWNOBJECT) and (playerName ~= UKNOWNBEING)) then
		return realmName.."."..playerName;
	end

	return "unknown";
end

-- Function: Moves items from one bag and slot to another bag and slot.
function LPS_SF_MoveItem(bag, slot, dBag, dSlot)
	ClearCursor();

	local _;

        _, _, locked1 = GetContainerItemInfo(bag, slot)
        _, _, locked2 = GetContainerItemInfo(dBag, dSlot)

	if ((locked1) and (locked2)) then
		return;
	end

	PickupContainerItem(bag, slot);
	PickupContainerItem(dBag, dSlot);

	--ClearCursor();
end

-- Function: Moves items from one bag and slot to another bag and slot.
function LPS_SF_MoveGBItem(tab, slot, dSlot)
	ClearCursor();

	local _;

        _, _, locked1 = GetGuildBankItemInfo(tab, slot)
        _, _, locked2 = GetGuildBankItemInfo(tab, dSlot)

	if ((locked1) and (locked2)) then
		return;
	end

	PickupGuildBankItem(tab, slot);
	PickupGuildBankItem(tab, dSlot);

	--ClearCursor();
end

-- Function: Convert a link to the item name
function LPS_SF_LinkToName(itemLink)
	if( itemLink ) then
		local _,_,name = string.find(itemLink, "^.*%[(.*)%].*$");
		return name;
	end
	return nil;
end

-- Function: Return the ID when given the item link
function LPS_SF_LinkToID(itemLink)
	if( itemLink ) then
		local _,_,id = string.find(itemLink, "^.*item:([0-9]+):.*$");	
		return id;
	end
	return nil;
end

-- Function: Find the tooltip line which contains itemString
function LPS_SF_GetItemTooltipLine(itemBag, itemSlot, itemString)
	local lineIndex		= 1;
	local tooltipLine	= nil;

	-- "Own" the LPS_ToolTip
	LPS_ToolTip:SetOwner(LPS_Frame);
	LPS_ToolTip:SetBagItem(itemBag, itemSlot);

	local myCheckLine = getglobal("LPS_ToolTipTextLeft"..lineIndex);
	while (myCheckLine) do
		local myText = myCheckLine:GetText();

		if (myText ~= nil) then
			if (string.find(myText, itemString) ~= nil) then
				tooltipLine = myText;
				LPS_ToolTip:Hide();
				return tooltipLine;
			end
		end

		lineIndex = lineIndex + 1;
		myCheckLine = getglobal("LPS_ToolTipTextLeft"..lineIndex);
	end
	LPS_ToolTip:Hide();
	return tooltipLine;
end

--[[

The function and details below was taken off the wowwiki site.
]]
function SaferSwapItems(bag1, slot1, bag2, slot2)
    ClearCursor()

    while true do                     -- This is not really an infinite loop, as you will see in a few lines

        local _, _, locked1 = GetContainerItemInfo(bag1, slot1)
        local _, _, locked2 = GetContainerItemInfo(bag2, slot2)

        if locked1 or locked2 then    -- During each iteration of the loop, we will either...
            coroutine.yield()         --     yield, thus returning from SaferSwapItems
        else                          --   or
            break                     --     break, thus returning from SaferSwapItems
        end
    end

    PickupContainerItem(bag1, slot1)
    PickupContainerItem(bag2, slot2)
end

LPS_ItemSwap = coroutine.create(SaferSwapItems);

--[[
function OnUpdate()
    coroutine.resume(co)    -- We should actually look at the return value from resume, because 
                            -- it will be false when the coroutine is actually finished
end
]]
